home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Passing Structures As Pointers; Easy Question I think....
- Date: 03 Mar 1996 19:43:27 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Mar3124327@qcd.lanl.gov>
- References: <4hcov3$h1d@newsbf02.news.aol.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: razine@aol.com's message of 3 Mar 1996 13:38:27 -0500
-
- In article <4hcov3$h1d@newsbf02.news.aol.com>
- razine@aol.com (Razine) writes:
-
- R: I recently ran into a problem passing a structure as a pointer to a
- R: function. I then wanted to pass a indivdual variable to another function
- R: but it didnt work. Here is a brief example. Can anyone spot anything
- R: wrong.
- R:
- R: typedef struct {
- R: char name[81];
- R: int address_num;
- R: } person_rec;
- R:
- R:
- R:
- R: void main(void) {
-
- main must be declared as returning int in a C program. The effect of
- returning void is undefined in the C standard.
-
- R: person_rec thisuser;
- R:
- R: display_person(&thisuser);
-
- As display_person has not yet been declared, the compiler assumes it
- is compatible with the declaration `int display_person()'. Your actual
- definition actually returns void, so this assumption is wrong. As the
- compiler makes a wrong assumption, no one can tell you what happens
- from here on. (The compiler may document what happens, but it is
- certainly not required to.) declare `void
- display_person(person_rec*);' before trying to use it, or move the
- definition to before the use.
-
- As display_person does not initialize the record before using it, you
- are using thisuser before initializing it. This is a disaster. I
- assume this is only because you were trying to make things, short and
- simple for us: please do not make things so short and simple that they
- actually become wrong!
-
- R:
- R: }
- R:
- R: void display_address(int address) {
- R: printf("Adress Number is %d\r\n",address);
-
- Use of \r in printf is almost certainly wrong. The C library is
- supposed to put in the correct line terminator when you printf a \n.
-
- R: return;
- R: }
- R:
- R: void display_person(person_rec *pr) {
- R:
- R: printf("Persons Name : %s\r\n",pr->name); /* another question why in
- R: this instance would I have to use the -> operand? */
-
- The operator -> means that the left operand is a pointer to a
- struct. The . operator means that the left operand is a struct.
-
- Remember %s assumes that the aaray of characters that you are sending
- in (loose usage) is terminated by a '\0'. If the array is not
- initialized, there is no guarantee what happens.
-
- R: display_address(pr->address_num);
- R:
- R: /* Basically I get an error on the line above, how would I just pass the
- R: interger value contained in pr->address_num */
-
- The call looks correct. What error do you get?
-
- R:
- R: return;
- R: }
- R:
- R:
- R: I hope someone can help, I found a work around but it is sloppy and I want
- R: to understand why, and what I am doing wrong. Thanks
- R:
-
- Only if you show us a small complete compilable example with an
- explanation of _what_ error you get when trying to compile or run it.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-